<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.sinodata.bsm.common.vo.Res"%>
<%@page import="com.sinodata.bsm.center.util.spring.SpringContextHolder"%>
<%@page import="com.sinodata.bsm.center.service.config.PortalService"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="com.sinodata.bsm.center.bean.ResBean"%>
<%@page import="com.sinodata.bsm.center.web.action.portlet.TopParamConstants"%>
<%@page import="com.sinodata.bsm.center.user.UserUtil"%>
<%@page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<%@page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@page import="com.sinodata.bsm.center.bean.RelationHelper" %>
<%
String param = request.getParameter("args");
JSONObject parameter="".equals(param)?null:JSONObject.fromObject(param);
%>
<script type="text/javascript">
	$(document).ready(function(){
		var valueSpan = $("span[name='value']");
		$.each(valueSpan,function(index,ob){
			var id = $(ob).attr('id');
			var value = $(ob).attr('value');
			$(ob).progressBar(value, { showText: true,barImage:'${pageContext.request.contextPath}/style/default/bsm/images/progressbg_green.gif'} );
		});
	});
</script>
<div>
	<div  class="blockDiv">
		<span class="menuTitle" style="width:30%;">主机名</span>
 	  	<span class="menuTitle" >主机IP</span>
 	  	<span class="menuTitle" style="width:50%; float:right;">使用率</span>
 	</div>
 <%
 	StringBuffer sql =new StringBuffer("select * from (select v.* from view_perf_topn v where top_type='MEM' and value is not null ");
 	UserUtil util= UserUtil.getInstance();
 	if(!util.isSysAdmin(util.getCurrentUserId())){
 	    List<Long> authScopeList = UserFunctionValidate.getResIdListForUser(FunctionConstants.VIEW_RES);
 	    StringBuffer currentUserResSql = new StringBuffer("(");
 	    for(int i=0;i<authScopeList.size();i++){
 	        Long resId = authScopeList.get(i);
 	        currentUserResSql.append(i==(authScopeList.size()-1)?resId+")":resId+",");
 	    }
 	    sql.append(" and res_id in (  select id from res where id in "+currentUserResSql.toString()+"  union  ");
 		sql.append(" select end_id from res_relation rel   start with start_id in "+currentUserResSql.toString() +" connect by prior end_id=start_id ) ");
 	}
 	
	String order = parameter==null?null:parameter.getString("PARAM_order");
	sql.append(" and time_range ='"+(parameter==null?TopParamConstants.TOPN_DATA_TIMERANGE:parameter.getString("PARAM_time"))+"'");
	//sql.append(" and rownum<="+(parameter==null?TopParamConstants.TOPN_DATA_PAGENUMBER:Integer.parseInt(parameter.getString("PARAM_limitsNum"))));
	if(order!=null){
		if(order.equals("high")){ //降序
			sql.append(" order by value desc ");
		}else{
			sql.append(" order by value asc ");
		}
	}else{
		sql.append(" order by value "+TopParamConstants.TOPN_DATA_ORDER);
	}
	
	sql.append(") where rownum<="+(parameter==null?TopParamConstants.TOPN_DATA_PAGENUMBER:Integer.parseInt(parameter.getString("PARAM_limitsNum"))));
 	PortalService service = SpringContextHolder.getBean(PortalService.class);
 	
 	
    // System.out.println(sql.toString());
     
     
 	List<Map<String,Object>> list = service.findListBySql(sql.toString());
 	for(int i=0;i<list.size();i++){
 	  Map<String,Object> map =list.get(i);
 	  Long resId = Long.parseLong(map.get("res_id".toUpperCase()).toString());
 	  ResBean bean =  ResBean.get(resId);
 	  if(bean==null){
 	      continue;
 	  }
 	  Res res = bean.res();
 	  String value = map.get("value".toUpperCase()).toString();
 	  Res parentRes = RelationHelper.parents(res.getId()).get(0).res();
%>
 	  <div class="valueDiv" style="background-color: <% if(i%2!=0){out.println("#F6F7F9");}else{out.println("#FFFFFF");} %>;line-height:20px;">
 	  	<span style="width:30%;display: block;float:left;  " title="<%=parentRes.getName() %>"><%=parentRes.getName() %></span>
 	  	<span><a href="${pageContext.request.contextPath}/resource/res-query!view.action?id=<%=res.getId() %>&pId=0&viewId=" target="_blank"><%=res.getIp() %></a></span>
 	  	<span style="float:right;width:49%;" id="value<%=i %>"  name="value" value="<%=value %>" ></span>
 	  </div>
 	  <%
 	}
%>
</div>
